/// <reference name="MicrosoftAjax.js" />
/// <reference name="dnn.js" assembly="DotNetNuke.WebUtility" />
/// <reference name="dnn.xmlhttp.js" assembly="DotNetNuke.WebUtility" />

Type.registerNamespace('OliverHine');

OliverHine.ViewQuicksand = function () {
    //Call Base Method
    OliverHine.ViewQuicksand.initializeBase(this);
    //Member Variables
    this._msgs = {};
    this._helloButton = null;
    this._responseLabel = null;
    this._actions = null;

    //Associated delegates to single member variable dictionary to make it easy to dispose
    this._delegates = {
        _fetchSuccessDelegate: Function.createDelegate(this, this._fetchSuccess),
        _fetchFailDelegate: Function.createDelegate(this, this._fetchFail),
        _onLoadDelegate: Function.createDelegate(this, this._onLoad),
        componentPropChangedDelegate: Function.createDelegate(this, this._onPropChanged)
    };

    //Event Hookup
    Sys.Application.add_load(this._delegates._onLoadDelegate);
}

OliverHine.ViewQuicksand.prototype =
{
    //Properties
    get_ns: function () { return this.get_id() + '_'; },
    get_msgs: function () { return this._msgs; },
    set_msgs: function (value) { this._msgs = value; },

    //Events
    initialize: function () {
        //Call Base Method    
        OliverHine.ViewQuicksand.callBaseMethod(this, 'initialize');

        this._responseLabel = $get(this.get_ns() + 'lblResponse');
        this._actions = $get(this.get_ns() + 'actions');

        //hookup event handlers
        if (this._actions) {
            $addHandlers(this._actions, { "click": this._onFetchPages }, this);
        }
        
    },

    _onLoad: function (src, arg) {
        //page is completely loaded, you can now access any element or component
        var components = arg.get_components();
        //sample of how to do client-side IMC - look for other components we are interested in
        //in this case, we will look for other instances of the same module that are not ourselves
        //but you can communicate with any components
        for (var i = 0; i < components.length; i++) {
            if (Object.getTypeName(components[i]) == Object.getTypeName(this) && components[i].get_id() != this.get_id())
                this.add_propertyChanged(components[i]._delegates.componentPropChangedDelegate);
        }
    },

    _onFetchPages: function (src, arg) {
        src.preventDefault();
        if (src.target.tagName == 'A') {

            var $parentItem = jQuery(src.target.parentNode);

            jQuery('#' + this.get_ns() + 'actions').find('li').removeClass('selected-0 selected-1 selected-2');

            if ($parentItem.hasClass('segment-0')) {
                $parentItem.addClass('selected-0');
            }
            else if ($parentItem.hasClass('segment-1')) {
                $parentItem.addClass('selected-1');
            }
            else if ($parentItem.hasClass('segment-2')) {
                $parentItem.addClass('selected-2');
            }

            this._displayWait(true);

            dnn.xmlhttp.callControlMethod('OliverHine.Modules.Quicksand.ViewQuicksand.' + this.get_id(),
                'FetchPages', { TagId: jQuery(src.target).attr('data-id') }, this._delegates._fetchSuccessDelegate, this._delegates._fetchFailDelegate);

            this.raisePropertyChanged('SayHello');
        }

    },

    _onPropChanged: function (src, args) {
        this.showMessage(String.format('You {0} to {1} but not to me?', args.get_propertyName(), src.get_name()));
    },

    //Methods
    getMessage: function (key) {
        return this._msgs[key];
    },

    showMessage: function (msg) {
        $get(this.get_ns() + 'lblResponse').innerHTML = msg;
    },

    //Private Methods
    _createChildControl: function (id, tag, type) {
        var ctl = document.createElement(tag);
        ctl.id = this.get_ns() + id;
        if (type)
            ctl.type = type;
        return ctl;
    },

    _displayWait: function (show) {
        $get(this.get_ns() + 'imgAjax').className = (show ? '' : 'ceHidden');
    },

    _fetchSuccess: function (payload, ctx, req) {
        this._displayWait(false);

        var $preferences = {
            duration: 800,
            easing: 'easeInOutQuad',
            adjustHeight: 'dynamic'
        };


        jQuery('ul.quicksand').quicksand(jQuery(payload).find('li'), $preferences);


        //this.showMessage(payload);
    },

    _fetchFail: function (payload, ctx, req) {
        this._displayWait(false);
        alert('error: ' + payload);
    },

    dispose: function () {
        //$clearHandlers(this._helloButton);
        this._helloButton = null;
        this._responseLabel = null;
        this._delegates = null;
        OliverHine.ViewQuicksand.callBaseMethod(this, 'dispose');
    }
}

//register class and inherit from Sys.Component
OliverHine.ViewQuicksand.registerClass('OliverHine.ViewQuicksand', Sys.Component);
